﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ECommerce.DAL;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace Entity
{
    public class ProductPartner
    {
        public Decimal PartnerProductId { get; set; }
        public Int32 PartnerId { get; set; }
        public Int32 ProductId { get; set; }
        public Decimal SellPrice { get; set; }
        public Decimal ProductionCost { get; set; }
        public String BrandName { get; set; }
    }

    public partial class TblPartnerProduct
    {
        public static CustomCollection<TblProduct> GetAllTblPartnerProductByPartnerName(String partnerName,String categoryName)
        {
            if (categoryName.EndsWith("/")) 
                categoryName = "~/" + categoryName;
            else
                categoryName = "~/" + categoryName+"/";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@partnerName", SqlDbType.VarChar, partnerName.Length, ParameterDirection.Input, partnerName);
            AddParameterToSqlCmd(sqlCmd, "@catName", SqlDbType.VarChar, categoryName.Length, ParameterDirection.Input, categoryName);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_TblPartnerProduct_GetTblPartnerProductByPartnerCategory");

            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblProductCollectionFromReaderExtended);
            CustomCollection<TblProduct> objCollection = ((CustomCollection<TblProduct>)ExecuteReaderCmd(sqlCmd, test));
            return objCollection;
        }

        public static CustomCollection<TblProduct> GetAllTblPartnerProductByPartnerName(String partnerName)
        {
            
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@partnerName", SqlDbType.VarChar, partnerName.Length, ParameterDirection.Input, partnerName);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_TblPartnerProduct_GetTblPartnerProductByPartnerName");

            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblProductCollectionFromReaderExtended);
            CustomCollection<TblProduct> objCollection = ((CustomCollection<TblProduct>)ExecuteReaderCmd(sqlCmd, test));
            return objCollection;
        }

        public static CustomCollection<TblProduct> GetAllFeaturedProductByPartnerName(String partnerName)
        {

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@partnerName", SqlDbType.VarChar, partnerName.Length, ParameterDirection.Input, partnerName);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_TblPartnerProduct_GetAllFeaturedProductByPartnerName");

            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblProductCollectionFromReaderExtended);
            CustomCollection<TblProduct> objCollection = ((CustomCollection<TblProduct>)ExecuteReaderCmd(sqlCmd, test));
            return objCollection;
        }

        public static CustomCollection<ProductPartner> GetAllTblPartnerProductByProductId(Int32 prodId)
        {

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@productId", SqlDbType.Int, 0, ParameterDirection.Input, prodId);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_TblPartnerProduct_GetAllTblPartnerProductByProductId");

            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateProductPartnerCollectionFromReaderExtended);
            CustomCollection<ProductPartner> objCollection = ((CustomCollection<ProductPartner>)ExecuteReaderCmd(sqlCmd, test));
            return objCollection;
        }

        public static TblProduct GetTblPartnerProductByPartnerName(String partnerName,Int32 prodId)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@partnerName", SqlDbType.VarChar, partnerName.Length, ParameterDirection.Input, partnerName);
            AddParameterToSqlCmd(sqlCmd, "@prodId", SqlDbType.Int, partnerName.Length, ParameterDirection.Input, prodId);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_TblPartnerProduct_GetTblPartnerProductByProductId");

            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblProductCollectionFromReaderExtended);
            CustomCollection<TblProduct> objCollection = ((CustomCollection<TblProduct>)ExecuteReaderCmd(sqlCmd, test));
            if (objCollection != null && objCollection.Count > 0)
            {
                return objCollection[0];
            }
            else
            {
                return null;
            }
        }


        public static CollectionBase GenerateTblProductCollectionFromReaderExtended(IDataReader returnData)
        {

            CustomCollection<TblProduct> colTblProduct = new CustomCollection<TblProduct>();

            
            while (returnData.Read())
            {
                //passing the TblProduct constructor parameters from the current instance of data reader fields.
                TblProduct newTblProduct = new TblProduct
                (
                    returnData["ProductId"] == System.DBNull.Value ? 0 : (Int32)returnData["ProductId"],
                    returnData["ProductName"] == System.DBNull.Value ? string.Empty : (String)returnData["ProductName"],
                    returnData["ProductName2"] == System.DBNull.Value ? string.Empty : (String)returnData["ProductName2"],
                    returnData["ProductDescription"] == System.DBNull.Value ? null : (String)returnData["ProductDescription"],
                    returnData["ProductImageUrl"] == System.DBNull.Value ? string.Empty : (String)returnData["ProductImageUrl"],
                    returnData["DisplayOrder"] == System.DBNull.Value ? string.Empty : (String)returnData["DisplayOrder"],
                    returnData["VisitCount"] == System.DBNull.Value ? 0 : (Int32)returnData["VisitCount"],
                    returnData["ProductUrl"] == System.DBNull.Value ? string.Empty : (String)returnData["ProductUrl"],
                    returnData["SellPrice"] == System.DBNull.Value ? 0 : (Decimal)returnData["SellPrice"],
                    returnData["OldPrice"] == System.DBNull.Value ? 0 : (Decimal)returnData["OldPrice"],
                    returnData["RetailPrice"] == System.DBNull.Value ? 0 : (Decimal)returnData["RetailPrice"],
                    returnData["AdditionalPrice"] == System.DBNull.Value ? 0 : (Decimal)returnData["AdditionalPrice"],
                    returnData["AdditionalPriceDesc"] == System.DBNull.Value ? string.Empty : (String)returnData["AdditionalPriceDesc"],
                    returnData["Recipies"] == System.DBNull.Value ? string.Empty : (String)returnData["Recipies"],
                    returnData["AdditionalInfo"] == System.DBNull.Value ? string.Empty : (String)returnData["AdditionalInfo"],
                    returnData["CreateDate"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["CreateDate"],
                    returnData["ItemNumber"] == System.DBNull.Value ? string.Empty : (String)returnData["ItemNumber"],
                    returnData["IsFeatured"] == System.DBNull.Value ? false : (Boolean)returnData["IsFeatured"],
                    returnData["IsFreeShipping"] == System.DBNull.Value ? false : (Boolean)returnData["IsFreeShipping"],
                    returnData["IsNewArrival"] == System.DBNull.Value ? false : (Boolean)returnData["IsNewArrival"],
                    returnData["IsActive"] == System.DBNull.Value ? false : (Boolean)returnData["IsActive"],
                    returnData["IsDeleted"] == System.DBNull.Value ? false : (Boolean)returnData["IsDeleted"],
                    returnData["IsClearenceItem"] == System.DBNull.Value ? false : (Boolean)returnData["IsClearenceItem"],
                    returnData["BrandId"] == System.DBNull.Value ? 0 : (Int32)returnData["BrandId"],
                    returnData["CategoryId"] == System.DBNull.Value ? 0 : (Int32)returnData["CategoryId"],
                    returnData["CurrentCost"] == System.DBNull.Value ? 0 : (Decimal)returnData["CurrentCost"],
                    returnData["ProductionCost"] == System.DBNull.Value ? 0 : (Decimal)returnData["ProductionCost"],
                    returnData["IsSpecial"] == System.DBNull.Value ? false : (Boolean)returnData["IsSpecial"]

                );
                //adding the Employee to the collection
                colTblProduct.Add(newTblProduct);
            }

            //returns the collection of Companys 
            return (colTblProduct);
        }

        public static CollectionBase GenerateProductPartnerCollectionFromReaderExtended(IDataReader returnData)
        {

            CustomCollection<ProductPartner> colTblProduct = new CustomCollection<ProductPartner>();

            while (returnData.Read())
            {
                ProductPartner brand = new ProductPartner();
               brand.BrandName = (String)returnData["BrandName"];
               brand.PartnerId = (Int32)returnData["PartnerId"];
               brand.PartnerProductId = (Decimal)returnData["PartnerProductId"];
               brand.ProductId = (Int32)returnData["ProductId"];
               brand.ProductionCost = (Decimal)returnData["ProductionCost"];
               brand.SellPrice = (Decimal)returnData["SellPrice"];
               colTblProduct.Add(brand);
            }

            //returns the collection of Companys 
            return (colTblProduct);
        }
    }
}